// pages/bookingorder/bookingorder.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageRoot: app.globalData.imageRoot,
    show: false,
    myl: false,
    actions: [{
        name: '默认排序'
      },
      {
        name: '价格最高'
      },
      {
        name: '价格最低'
      },
      {
        name: '最新发布'
      },
      {
        name: '折扣率最低'
      }
    ],
    active: 0,
    status: 0,
    stats: ["不限", "待审核", "已取消", "待评价", "已完成"],
    value1: 0,
    list: [],
    page_token: 0
  },
  timeReset() {
    this.setData({
      page_token: 0
    })
    var data = {
      page_token: 0,
      page_size: 10
    }
    this.api(data)
    this.selectComponent('#item2').toggle();
  },
  preventdefault: function () {},
  areaReset(e) {
    let that = this;
    that.setData({
      page_token: 0
    })
    var data = {
      page_size: 10,
      page_token: 0
    }
    that.api(data)
    that.selectComponent('#item3').toggle();
  },
  onReachBottom() { //上拉加载更多
    var that = this;
    //检查是否还有数据可以加载
    var total_size = that.data.list.total;
    var size = that.data.list.results.length;
    var page_token = that.data.page_token
    console.log(total_size)
    //如果还有，则继续加载
    if (total_size > size) {
      var data = {
        page_size: 10,
        ordertype: that.data.active,
        page_token: page_token += 1,
      }
      that.api(data)
      //如果没有了，则停止加载，显示没有更多内容了
    } else {
      that.setData({
        myl: true
      })
    }
    that.setData({
      page_token: page_token
    })
    console.log(size, '===', total_size)
    console.log(that.data.fpmyl)
  },
  areaInEnter(e) {
    var that = this;
    var ordertype = that.data.active;
    var reg = /[\u4e00-\u9fa5]/g;
    var time = e.detail.years + '-' + e.detail.months + '-' + e.detail.days;
    var create_time = time.replace(reg, "")
    var data = {
      ordertype: ordertype,
      create_time: create_time,
      page_size: 10,
      page_token: 0
    }
    that.api(data)
    that.selectComponent('#item2').toggle();
  },
  inEnter(e) {
    var that = this;
    console.log(e.detail);
    var data = {
      ordertype: that.data.active,
      province: e.detail.province.value,
      city: e.detail.city.value,
      district: e.detail.area.value,
      page_size: 10,
      page_token: 0
    }
    that.selectComponent('#item3').toggle();
    that.api(data)
  },
  onChange(event) {
    var that = this;
    Object.assign(that.data,{
      page_token: 0,
    })
    that.setData({
      active: event.detail.index,
      myl:false
    })
    var data = {
      ordertype: event.detail.index,
      page_size: 10,
      page_token: 0
    }
    that.api(data)
  },
  qyMore(e) { //签约状态筛选
    var that = this;
    console.log(that.data.status)
    var data = {
      page_size: 10,
      page_token: 0
    }
    that.setData({
      page_token:0,
      status:that.data.status
    })
    that.api(data)
    that.selectComponent('#item1').toggle();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      active_color:app.globalData.active_color
    })
    var data = {
      page_size: 10,
      page_token: 0
    }
    this.api(data);
  },
  api(data) {
    wx.showLoading({
      title: '加载中',
    })
    if(data.status) data.oStage = that.data.status;
    var that = this;
    var active = that.data.active;
    getApp().ports.api19({
      data: data
    }).then((res) => {
      var res = res.data;
      var arr = ['市场专员','合伙专员']
      if (data.page_token >= 1) {
        res.results.forEach(item => {
          item.active = 1;
          item.createTime = app.timeFormat(item.createTime, true)
          switch (item.type) {
            case 1:
              item.url = '../../subpackages/pages/fpsign/fpsign?status=1&id=' + item.id
              break;
            case 2:
              item.banner = that.data.imageRoot + item.banner;
              item.url = '../../subpackages/pages/discount/discount?status=2&id=' + item.id
              break;
            case 3:
              // item.title = item.discount_house__title + ' ' + 
              if (item.banner) {
                item.banner = that.data.imageRoot + item.banner
              }else{
                item.banner = 'https://api.yipaiju.com/resources/images/wawa/index_1.jpg';
              }
              if(item.stage <= 2 && arr.includes(wx.getStorageSync('identity')) && wx.getStorageSync('userId') == item.marketer){
                // 这里原跳转报错 也没有明白为什么要跳到这里  暂时先不管
                // item.url = '../listing/listing?id=' + item.id
                // 不知道为啥这里总是跳上面的url
                item.url = '/subpackages/pages/listing/listing?id=' + item.id
              }else{
                item.url = '../fplisting/fplisting?status=3&id=' + item.id
              }
              break;
            case 4:
              // res.results.splice(index, 1)
              break;
          }
          that.data.list.results.push(item)
        })
        res.results = that.data.list.results
      } else {
        res.results.forEach((item,index) => {
          item.createTime = app.timeFormat(item.createTime, true)
          item.active = 1;
          switch (item.type) {
            case 1:
              item.url = '../../subpackages/pages/fpsign/fpsign?status=1&id=' + item.id
              break;
            case 2:
              item.banner = that.data.imageRoot + item.banner;
              item.url = '../../subpackages/pages/discount/discount?status=2&id=' + item.id
              break;
            case 3:
              // item.title = '111111'
              if (item.banner) {
                item.banner = that.data.imageRoot + item.banner
              }else{
                item.banner = 'https://api.yipaiju.com/resources/images/wawa/index_1.jpg';
              }
              if(item.stage <= 2 && arr.includes(wx.getStorageSync('identity')) && wx.getStorageSync('userId') == item.marketer){
                item.url = '/subpackages/pages/listing/listing?id=' + item.id
              }else{
                item.url = '../fplisting/fplisting?status=3&id=' + item.id
              }
              break;
            case 4:
              // res.results.splice(index, 1)
              break;
          }
        })
      }
      that.setData({
        list: res,
      })
      wx.hideLoading({
        success: (res) => {},
      })
      // that.userInfo(res)
    }).catch(() => {
      console.error('出错');
    });

  },
  userInfo(user) {
    var that = this;
    var list = that.data.list;
    let liky = user.results.filter((p) => {
      return p.client
    })
    let v = [];
    for (let i = 0; i < liky.length; i++) {
      v[i] = liky[i].client
    }
    getApp().ports.api27({
      data: v
    }).then((res) => {
      var res = res.data;
      console.log(list)
      for (let i in user.results) {
        for (let j in res) {
          if (user.results[i].client == res[j].id) {
            user.results[i].markName = res[j].nickname;
            user.results[i].avatar_url = res[j].avatar_url;
            user.results[i].telphone = res[j].phone;
          }
        }
      }
      that.setData({
        list: user
      })
      console.log(that.data.list, '========')
    }).catch(() => {
      console.error('出啥错了？');
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page_token:0
    })
    this.api({ ordertype: this.data.active,});
    setTimeout(() => {
      wx.stopPullDownRefresh()
      console
    }, 1000)
  },
 
  statbtn(e) {
    console.log(e.currentTarget.dataset.index)
    var i = e.currentTarget.dataset.index;
    this.setData({
      status: i
    })
  },
  suspenRouter() {
    wx.navigateTo({
      url: '/pages/launchContract/launchContract',
    })
  },
  onSwitch1Change({
    detail
  }) {
    this.setData({
      switch1: detail
    });
  },
  onSwitch2Change({
    detail
  }) {
    this.setData({
      switch2: detail
    });
  },
  Jdiction() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  search(e) {
    console.log(e.detail)
    var ordertype = this.data.active;
    var data = {
      ordertype: ordertype,
      page_size: 10,
      page_token: 0,
      title: e.detail
    }
    this.api(data)
  }
})